# coding=utf-8
if __name__ == '__main__':
    def getavgpos(listobj):
        """
        获取列表中位置的均值坐标
        :param listobj: like:[(1, 1), (2, 1), (4, 1), (9, 1), (10, 2)]
        :return: 
        """
        if listobj is None:
            return None
        sumx = sumy = 0
        for item in listobj:
            if len(item) != 2:
                break
            sumx += item[0]
            sumy += item[1]
        return sumx // len(listobj), sumy // len(listobj)


    def getdistance(targetpos, srcpos):
        """
        获取两个位置的距离，或一个位置列表与一个位置的总距离
        :param targetpos: 
        :param srcpos: 
        :return: 
        """
        if not isinstance(srcpos, tuple):
            return -1
        if isinstance(targetpos, tuple):
            return abs(targetpos[0] - srcpos[0]) + abs(targetpos[1] - srcpos[1])
        if isinstance(targetpos, list):
            sumd = 0
            for item in targetpos:
                sumd += abs(item[0] - srcpos[0]) + abs(item[1] - srcpos[1])
            return sumd


    def __distancefromorigin(poslist):
        """
        获取位置列表中各个位置距离原点（0,0）的距离
        :param poslist: 
        :return: 字典类型数据（key=index，value= 距离）
        """
        if isinstance(poslist, tuple):
            return abs(poslist[0] - 0) + abs(poslist[1] - 0)
        if isinstance(poslist, list):
            dic = {}
            for index, item in enumerate(poslist):
                dic[index] = abs(item[0] - 0) + abs(item[1] - 0)
            # tuple in list object
            sortedres = sorted(dic.items(), key=lambda e: e[1], reverse=True)
            return sortedres


    def getcloseNpos(n, poslist):
        """
        去除位置列表中，离原点（0，0）最远的N个点
        :param n: 
        :param poslist: 
        :return: 
        """
        distance = __distancefromorigin(poslist)
        if n > len(distance):
            n = len(distance)
        newlist = []
        for i in range(n):
            newlist.append(poslist[distance[len(distance) - i - 1][0]])
        return newlist


    def geteachdistance(poslist):
        """
        获取一个位置列表中两两位置的距离，结果去重并排序
        :param poslist: 
        :return: 
        """
        dis = []
        for i in range(len(poslist)):
            for j in range(i + 1, len(poslist)):
                dis.append(abs(poslist[i][0] - poslist[j][0]) + abs(poslist[i][1] - poslist[j][1]))
        return list(set(dis))


    # TEST
    # pos = [(4, 1), (1, 1), (3, 1), (2, 1)]
    pos = [(1, 1), (3, 1), (2, 1), (4, 1), (9, 1), (10, 2)]
    print "#####################"
    length = len(pos)
    result1 = 0
    result2 = geteachdistance(pos)[0]
    avgpos = getavgpos(pos)
    resultall = getdistance(pos, avgpos)  # all in a pos  
    print "result1=" + str(result1)
    print "result2=" + str(result2)
    if length > 2:
        for i in range(3, length):
            tmppos = getcloseNpos(i, pos)
            # print "tmppos:" + str(tmppos)
            avgpos = getavgpos(tmppos)
            # print avgpos
            print "result" + str(i) + "=" + str(getdistance(tmppos, avgpos))
    print "result" + str(length) + "=" + str(resultall)
